<script type="text/javascript">
$(document).ready(function() {
	var address;
	$('#estado').trigger("change");
});

$('#cepShip').live('blur', function(){
	url = "/address/get-address?cep="+$(this).val(),
	
	$.get(url, function(data){
		address = data;
		if(data != null){
			$('#ende').val(address.address_type+" "+address.address);

			$('#bairro').val(address.neighborhood);

			if (data != null) {
				$('#estado option[value="' + address.state + '"]').attr({
					selected : "selected"
				});

			} else {
				$('#estado option[value=""]').attr({
					selected : "selected"
				});
			}

			$('#estado').trigger("change");

			if (data != null) {
				$('#cityShip option[value="' + address.city + '"]').attr({
					selected : "selected"
				});
			} else {
				$('#cityShip option[value=""]').attr({
					selected : "selected"
				});
			}
		}else{
			$('#ende').val("");
			
			$('#bairro').val("");

			$('#estado option[value=""]').attr({
				selected : "selected"
			});

			$('#cityShip option[value=""]').attr({
				selected : "selected"
			});
		
			alert('CEP não encontrado!');
		}
	}, 'json');

	url='/cart/show-payment?cep='+$("#cepShip").val()+'&city='+$('#cityShip').val();

		$.get(url, function(data){
			$('#shipingForm').html($(data).find('#shipingForm').html());
		});
})

	$('#confirm').live('click', function(){

		if($('#shippingType').val() == null || $('#shippingType').val() == "" && $('#cityShip').val() == ""){
			alert("Selecione o tipo de Frete!\nSelecione a Cidade!");
		}
		else if($('#shippingType').val() == null || $('#shippingType').val() == ""){
			alert("Selecione o tipo de Frete!");
		}
		else if($('#cityShip').val() == ""){
			alert('Selecione a Cidade!')
		}
		else{
			var totalFields = document.sendPayment.elements.length;
			for (i = 1; i < totalFields; i++) {
				document.sendPayment.elements[i].disabled = false;
			}
			$('#sendPayment').submit();
		}
	});

	$('.payment').live('click', function(){
		$('.text').removeClass('checked');
		$('.text').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#paymentType').val($(this).prev().val());

		if($(this).prev().val() == 1){
			$('.pay-form').hide();
			$('#card-form').show();
		}

		if($(this).prev().val() == 2){
			$('.pay-form').hide();
			$('#debit-form').show();
		}

		if($(this).prev().val() == 3){
			$('.pay-form').hide();
			$('#billet-form').show();
		}
	})
	
	$('.ship').live('click', function(){
		$('.ship').removeClass('checked');
		$('.ship').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#shippingType').val($(this).prev().val());
	})
	
	$('#chaddr').live('click', function(){
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		var totalFields = document.sendPayment.elements.length;
			for (i = 1; i < totalFields; i++) {
				document.sendPayment.elements[i].disabled = false;
			}
	});

	$('#estado').live('change', function(){
		url='/address/get-cities?state='+$(this).val();

		$.get(url, function(data){
			$('#cityShip').html(data);

			if(address != null && address.address != undefined){
				$('#cityShip option[value="' + address.city + '"]').attr({
					selected : "selected"
				});
			}else{
				$('#cityShip option[value="'+$('#cityC').val()+'"]').attr({
					selected : "selected"
				});
			}
		})
	});

	$('#cityShip').live('change', function(){
		url='/cart/show-payment?cep='+$("#cepShip").val()+'&city='+$('#cityShip').val();

		$.get(url, function(data){
			$('#shipingForm').html($(data).find('#shipingForm').html());
		})
	});
	
</script>

<div id="list-cart" style="width: 850px; height: 260px;">
	<div id="payment-form">
		<!--<input type="hidden" id="cardId" name="cardId" value>
		<div style="width: 75%; height: 50px; padding-left: 25%; margin-bottom: 5px;">

			<div style="float: left">
				<input style="margin-top: 10px; margin-left: 25px;" type="radio"
					id="credit" name="payment" value="1" /> <label for="credit"
					class="text checked payment" style="margin-left: 10px; font-family: amerika">&nbsp Cartão de
					Crédito</label>
			</div>
			<div style="float: left">
				<input style="margin-top: 10px; margin-left: 15px;" type="radio"
					id="debit" name="payment" value="2" /> <label for="debit"
					class="text unchecked payment" style="margin-left: 10px; font-family: amerika">&nbsp Débito em
					Conta</label>
			</div>
			<div style="float: left">
				<input style="margin-top: 10px; margin-left: 15px;" type="radio"
					id="billetCheck" name="payment" value="3" /> <label
					for="billetCheck" class="text unchecked payment"
					style="margin-left: 10px; font-family: amerika">&nbsp Boleto Bancário</label>
			</div>
		</div> -->

		<div id="card-form" class="pay-form" style="display: block; height: 150px">
			<form name="sendPayment" id="sendPayment" method="post" action="/bought/index">
				<div id="shipingForm" style="width: 100%; height: 90px; padding-left: 5%; margin-bottom: 5px;">
					<div class="formLeft" style="float: left">
						<?php if($this->isHere):?>
							<input type="hidden" id="shippingType" name="shippingType" value="3">
							<div style="width: 405px; height: 30px; float: left; line-height: 30px;">
								Horário para Entrega: 
								<select name="deliveryHour" id="deliveryHour">
									<option>entre 9:00 e 10:00</option>
									<option>entre 10:00 e 11:00</option>
									<option>entre 11:00 e 12:00</option>
									<option>entre 12:00 e 13:00</option>
									<option>entre 13:00 e 14:00</option>
									<option>entre 14:00 e 15:00</option>
									<option>entre 15:00 e 16:00</option>
									<option>entre 16:00 e 17:00</option>
									<option>entre 17:00 e 18:00</option>
								</select>
							</div>
							<div style="width: 405px; height: 30px; float: right; line-height: 30px;" title="Sedex">
								Quem vai Receber: <input style="width: 250px;" type="text" name="receptor" id="receptor">
							</div>
							<div style="margin-top: 40px;">
								<label>*Pode variar de acordo com a disponibilidade</label>
							</div>
						<?php else:?>
							<input type="hidden" id="shippingType" name="shippingType">
							<div style="width: 405px; height: 30px; float: left; line-height: 30px;">
								<div style="height: 45px;">
									<img width="60" src="/images/pac.jpg" title="PAC" style="float: left">
									<input id="pac" value="1" class="card-buttom" name="ship" class="formLeft" type="radio" >
									<label id="pc" for="pac" class="ship unchecked" style="margin-top: 12px; margin-left: 5px; padding: 0 0 0 18px;">Entrega via PAC (3 a 5 dias úteis*)</label>
								</div>

								<div style="height: 45px;">
									<img width="60" src="/images/sedex.jpg" style="float: left">
									<input id="sedex" value="2" name="ship" class="formLeft" type="radio">
									<label id="sdx" for="sedex" class="ship unchecked" style="margin-top: 4px; margin-left: 5px; padding: 0 0 0 18px;">Entrega via SEDEX (1 a 3 dias úteis*)</label>
								</div>
							</div>
							<div style="width: 405px; height: 30px; float: right; line-height: 30px;" title="Sedex">
								<div style="float: right;">
									<!-- INICIO CODIGO PAGSEGURO -->
									<a href="https://pagseguro.uol.com.br/desenvolvedor/simulador_de_frete.jhtml?CepOrigem=14800480&amp;CepDestino=<?php echo $this->client['zipcode']?>&amp;Peso=<?php echo number_format($this->weight, 3)?>&amp;Valor=<?php echo number_format($this->price, 2, ',', '.')?>" id="ps_freight_simulator" target="_blank"><img src="https://p.simg.uol.com.br/out/pagseguro/i/user/imgCalculoFrete.gif" id="imgCalculoFrete" alt="Cálculo automático de frete" border="0" /></a>
									<script type="text/javascript" src="https://p.simg.uol.com.br/out/pagseguro/j/simulador_de_frete.js"></script>
									<!-- FINAL CODIGO PAGSEGURO -->
								</div>
							</div>
							<div style="margin-top: 70px;">
								<label>*Pode variar de acordo com sua loalização</label>
							</div>
						<?php endif;?>
					</div>
				</div>
				
				<div style="width: 100%; height: 85px; padding-left: 5%; margin-top: 15px;">
						<h1 style="margin-bottom: 15px">Dados do Comprador</h1>
					<div style="float: left">
						<label style="font-size: 20px;"><b>Nome:</b><?php echo $this->client['name']?></label>
					</div>
					<div style="float: left; margin-left: 10px;">
						<label style="font-size: 20px;"><b>Email:</b><?php echo $this->client['email']?></label>
					</div>
					<div style="float: left; margin-left: 10px;">
						<label style="font-size: 20px;"><b>CPF:</b><?php echo $this->client['cpf']?></label>
					</div>
					<div style="float: left; margin-left: 10px;">
						<label style="font-size: 20px;"><b>Telefone:</b><?php echo $this->client['phone']?></label>
					</div>
					
					<div style="float: left;">
						<label style="font-size: 20px;"><b>Data de Nascimento:</b><?php echo $this->client['birth']?></label>
					</div>
				</div>
				
				
				<div style="width: 100%; height: 55px; padding-left: 5%; margin-bottom: 5px;">
					<div style="float: left; margin-bottom: 15px; width: 100%; height: 20px;">
					<h1 style=" float: left;">Endereço de Envio</h1>
					<input id="changeAddress" class="card-buttom" type="radio" >
					<label id="chaddr" for="changeAddress" class="unchecked" style="margin-left: 5px; padding: 0 0 0 18px; float: left;">Alterar Endereço de envio</label>
					</div>
					<div style="float: left">
						CEP:<input disabled="disabled" onkeyup="maskCep(this)" type="text" name="cepShip" id="cepShip" value="<?php echo $this->client['zipcode']?>" style="width: 150px;">
					</div>
					<div style="float: left; margin-left: 10px;">
						Endereço:<input disabled="disabled" type="text" name="addressShip" id="ende" value="<?php echo $this->client['address']?>" style="width: 300px;">
					</div>
					<div style="float: left; margin-left: 10px;">
						Nº:<input disabled="disabled" type="text" name="numberShip" id="number" value="<?php echo $this->client['number']?>" style="width: 150px;">
					</div>
				</div>
				
				<div style="width: 100%; height: 20px; padding-left: 5%; margin-bottom: 5px; margin-top: 20px;">
					<div style="float: left;">
						Complemento:<input disabled="disabled" type="text" name="compShip" id="comp" value="<?php echo $this->client['comp']?>" style="width: 200px;">
					</div>
					<div style="float: left; margin-left: 10px;">
						Bairro:<input disabled="disabled" type="text" name="neighShip" id="bairro" value="<?php echo $this->client['neighborhood']?>" style="width: 200px;">
					</div>
					<div style="float: left; margin-left: 10px;">
						Estado:
						<select disabled="disabled" name="stateShip" id="estado">
							<option value="">Selecione</option>
							<?php foreach ($this->states as $state):?>
								<option <?php if($this->client['state'] == $state['des_state']):?>selected="selected"<?php endif;?> value="<?php echo $state['des_state']?>"><?php echo $state['des_state']?></option>
							<?php endforeach;?>
						</select>
					</div>
				</div>
				
				<div style="width: 100%; height: 55px; padding-left: 5%; margin-top: 20px;">
					<div style="float: left;">
					Cidade:
						<select disabled="disabled" name="cityShip" id="cityShip">
							<option value="">Selecione</option>
							<?php foreach ($this->cities as $city):?>
								<option <?php if($this->client['city'] == utf8_encode($city['des_city'])):?>selected="selected"<?php endif;?> value="<?php echo  utf8_encode($city['des_city'])?>"><?php echo  utf8_encode($city['des_city'])?></option>
							<?php endforeach;?>
						</select>
					</div>
				</div>
			</form>
		</div>	
		
		<br clear="all" />
	</div>
</div>

<div style="height: 50px; margin-top: 100px;">
			<div class="formLeft" style=" float: right">
				<div id="confirm" style="width: 130px; height: 20px; background:white; margin-left: 10px; margin-top: 49px; cursor: pointer; color: #000">
					Confirmar
				</div>
			</div>
			<div class="formLeft" style=" float: right">
				<div style="width: 130px; height: 20px; background:white;">
					<b style="color: #000; line-height: 18px; font-size: 16px">Total da Compra</b>
				</div>
				<div style="widows: 130px; height: 35px; padding-top: 10px; margin-top: 5px; background:white;">
					<b style="color: black; font-size: 30px; line-height: 15px; padding-left: 20px; font-family: amerika;">
						<?php echo 'R$'.number_format($this->price, 2, ',', '.')?>
					</b>
					<br />
					<b style="color: black; font-size: 15px; float: right;">+ frete</b>
				</div>
			</div>
		</div>
